package org.soenea.doitall.domain.command;

import org.dsrg.soenea.domain.MapperException;
import org.dsrg.soenea.domain.command.CommandError;
import org.dsrg.soenea.domain.command.CommandException;
import org.dsrg.soenea.domain.command.DomainCommand;
import org.dsrg.soenea.domain.helper.Helper;
import org.dsrg.soenea.service.SingleAppUniqueIdFactory;
import org.soenea.doitall.domain.model.person.Person;
import org.soenea.doitall.domain.model.person.data.PersonTDG;
import org.soenea.doitall.domain.model.person.mapper.PersonInputMapper;
import org.soenea.doitall.domain.model.person.mapper.PersonOutputMapper;

public class AddPersonCommand extends DomainCommand {

	public AddPersonCommand(Helper helper) {
		super(helper);
	}

	@Override
	public void setUp() throws CommandException {
	}

	@Override
	public void process() throws CommandException {
		try {
			String userName = helper.getString("username");
			String password = helper.getString("password");
			int groupNum = 0;
			String firstName = helper.getString("firstName");
			String lastName = helper.getString("lastName");
			long id = 0L;
			try{
				id = SingleAppUniqueIdFactory.getMaxId(PersonTDG.BASE_NAME, PersonTDG.userID);
			}catch( Exception e){
				//supress exception
			}
			Person person = new Person(id, userName, password, groupNum,
					firstName, lastName, 0L);
			PersonOutputMapper.insert(person);
			helper.setRequestAttribute("people", PersonInputMapper.findAll());
		} catch (MapperException e) {
			throw new CommandException(e);
		}
	}

	@Override
	public void tearDown() throws CommandError {
	}

}
